<?php

namespace app\controllers;

use app\models\SysUser;
use Yii;
use yii\helpers\Json;

class LoginController extends \yii\web\Controller
{
    public $layout = 'basic';

    public function actionIndex()
    {
        return $this->renderPartial('index');
    }

    public function actionLogin()
    {
        if ( ! Yii::$app->request->isAjax) {
            return false;
        }
        $request = Yii::$app->request->post(); //获取$_POST
        $user_account = $request['user_account']; //默认缺省值，即没有获取到时候，返回null
        $user_passwd = $request['user_passwd']; //缺省值为''
        $m_user = new SysUser();
        $user = $m_user->findByUserAccount($user_account);
        if($user == null || ! $m_user->validatePassword($user,$user_passwd))
        {
            return Json::Encode([
                "errCode"=>1,
                "msg"=>"账户或密码错误！"
            ]);
        }

        if('active' != $user['user_status'] )
        {
            return Json::Encode([
                "errCode"=>1,
                "msg"=>"账户已停机！"
            ]);
        }

        $userid = Yii::$app->user->login($user, 3600);
        return Json::Encode([
            "errCode"=>0,
            "msg"=>"Success！"
        ]);

    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}